limsZkSample.editPage = {

    getZkSampleParam : function(){
        let _this = this;
        return{
            type : 'grid',
            id : 'lims-zk-sample-edit-list-grid',
            url : limsZkSample.baseUrl + '/queries/raw',
            service : 'limsZkSampleServiceImpl',
            columns : limsZkSample.getZkSampleColumns(),
            page : true,
            deleteFormatter : function (row) {
                return row.zkTypeCode
            },
            onLoadSuccess : function (data) {
                if (Gikam.isNotEmpty(data)){
                    this.activeRowByIndex(0)
                }else{
                    Gikam.getComp('lims-zk-sample-template-list-grid').cleanData();
                    Gikam.getComp('lims-zk-sample-template-test-list-grid').cleanData();
                }
            },
            onRowActive : function (index, row) {
                _this.loadZkSampleList(row);
            },
            toolbar : [{
                type : 'button',
                text: 'GIKAM.BUTTON.INSERT',
                icon: 'add',
                onClick : function () {
                    _this.addZkSample();
                }
            },{
                type: 'button',
                text: 'GIKAM.BUTTON.DELETE',
                icon: 'remove-row',
                onClick : function () {
                    _this.delZkSample();
                }
            }]
        }
    },

    addZkSample : function(){
        Gikam.preInsert({
            modalTitle: 'GIKAM.BUTTON.INSERT',
            fields : limsZkSample.getZkSampleFields(),
            url : limsZkSample.baseUrl,
            height : 250,
            width : 400,
            titleWidth : 120
        }).done(function () {
            Gikam.getComp('lims-zk-sample-edit-list-grid').refresh();
        })
    },

    delZkSample : function(){
        Gikam.getComp('lims-zk-sample-edit-list-grid').deleteRows(limsZkSample.baseUrl);
    },

    loadZkSampleList : function(row){
        Gikam.getAsyncComp('lims-zk-sample-template-list-grid').done(function (grid) {
            grid.refresh({
                url : limsZkSample.templateUrl + '/queries',
                requestData : {
                    zkSampleId_EQ : row.id
                }
            })
        })
    },

    getZkSampleTemplateParam : function(){
        let _this = this;
        return{
            type : 'grid',
            id : 'lims-zk-sample-template-list-grid',
            url : limsZkSample.templateUrl + '/queries/raw',
            service : 'limsZkSampleTemplateServiceImpl',
            columns : limsZkSample.getZkSampleTemplateColumns(),
            page : true,
            deleteFormatter : function (row) {
                return row.ext$.etname
            },
            onLoadSuccess : function (data) {
                if (Gikam.isNotEmpty(data)){
                    this.activeRowByIndex(0)
                }else{
                    Gikam.getComp('lims-zk-sample-template-test-list-grid').cleanData();
                }
            },
            onRowActive : function (index, row) {
                _this.loadZkSampleTemplateList(row);
            },
            toolbar : [{
                type : 'button',
                text: 'GIKAM.BUTTON.INSERT',
                icon: 'add',
                onClick : function () {
                    _this.addZkSampleTemplate();
                }
            },{
                type: 'button',
                text: 'GIKAM.BUTTON.DELETE',
                icon: 'remove-row',
                onClick : function () {
                    _this.delZkSampleTemplate();
                }
            }]
        }
    },

    loadZkSampleTemplateList : function(row){
        Gikam.getAsyncComp('lims-zk-sample-template-test-list-grid').done(function (grid) {
            grid.refresh({
                url : limsZkSample.templateTestUrl + '/queries',
                requestData : {
                    etId_EQ : row.spId
                }
            })
        })
    },

    addZkSampleTemplate : function(){
        let row = Gikam.getComp('lims-zk-sample-edit-list-grid').getActivedRow();
        if (Gikam.isEmpty(row)){
            Gikam.alert('GIKAM.TIP.CHOOSE_AT_LEAST_ONE_ITEM');
            return;
        }
        Gikam.preInsert({
            modalTitle: 'GIKAM.BUTTON.INSERT',
            fields : limsZkSample.getZkSampleTemplateFields(),
            url : limsZkSample.templateUrl,
            titleWidth : 120,
            param : {
                zkSampleId : row.id
            },
            disableNotEmptyValidate : true
        }).done(function () {
            Gikam.getComp('lims-zk-sample-template-list-grid').refresh();
        })
    },

    delZkSampleTemplate : function(){
        Gikam.getComp('lims-zk-sample-template-list-grid').deleteRows(limsZkSample.templateUrl);
    },

    getZkSampleTemplateTestParam : function(){
        let _this = this;
        return {
            type: 'grid',
            id: 'lims-zk-sample-template-test-list-grid',
            order : {orderNo : 'asc'},
            columns: limsZkSample.getZkSampleTemplateTestColumns(),
            onLoadSuccess: function (rows) {
            },
            onRowActive: function (index, row) {
            },
            toolbar: []
        }
    },

    create : function() {
        let _this = this;
        Gikam.create('layout', {
            id : 'lims-zk-sample-edit-list-layout',
            renderTo : workspace.window.$dom,
            west : {
                width : '40%',
                title : 'T_LIMS_ZK_SAMPLE.SERVER_NAME',
                items : [ this.getZkSampleParam() ]
            },
            center : {
                items : [{
                    type : 'layout',
                    north : {
                        height : '40%',
                        title : 'T_LIMS_EXPERIMENTAL_TEMPLATE.SERVICE_NAME',
                        items : [this.getZkSampleTemplateParam()]
                    },
                    center : {
                        title : 'T_LIMS_EXPERIMENTAL_TEMPLATE_TEST.SERVICE_NAME',
                        items : [this.getZkSampleTemplateTestParam()]
                    }
                }]
            }
        });
    },

    init : function() {
        this.create();
    }
};
